package com.example.test_application;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.example.custom_widget.flowImageView;
import com.example.game_logic.Bloc;
import com.example.game_logic.Bloc.Cardinality;
import com.example.game_logic.Color.ColorID;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	private int gridSize_;
	
	// references to our images
	private LayerDrawable[][] drawables_;

	public ImageAdapter(Context c) {
		mContext = c;
	}

	public int getCount() {
		return gridSize_*gridSize_;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		flowImageView imageView;
		if (convertView == null) {  // if it's not recycled, initialize some attributes
			imageView = new flowImageView(mContext);
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageView.setPadding(0, 0, 0, 0);
		} else {
			imageView = (flowImageView) convertView;
		}

		int[] tempCoord = GridActivity.position2Coord(position, gridSize_);

		imageView.setImageDrawable(drawables_[tempCoord[0]][tempCoord[1]]);
		return imageView;
	}

	public void initImages(int gridSize)
	{
		gridSize_ = gridSize;
		drawables_ = new LayerDrawable[gridSize_][gridSize_];
		
		Resources r = mContext.getResources();
		Drawable[] layers = new Drawable[1];
		layers[0] = r.getDrawable(R.drawable.empty);
		LayerDrawable layerDrawable2 = new LayerDrawable(layers);
		
		for (int i = 0 ; i < gridSize_ ; i++)
		{
			for (int j = 0 ; j < gridSize_ ; j++)
			{		
				drawables_[i][j] = layerDrawable2;
			}
		}
	}

	public void setImage(int x, int y, Bloc bloc)
	{
		ColorID color = bloc.getColor();
		boolean isExtremity = bloc.isExtremity();
		boolean isUsed = bloc.isUsed();
		
		List<Cardinality> cardBlocs = new ArrayList<Cardinality>();
		cardBlocs.add(bloc.getChildCard());
		cardBlocs.add(bloc.getParentCard());
		
		Resources r = mContext.getResources();
		int sizeLayer = 3;
		Drawable[] layers = new Drawable[sizeLayer];
		int currentLayerUp = sizeLayer-1;
		
		for (int i = 0 ; i < sizeLayer ; i++)
		{
			layers[i] = r.getDrawable(R.drawable.empty);
		}

		if (color == ColorID.RED)
		{			
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_red_used);
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_red);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_red_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_red_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_red_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_red_o);
				}	
			}
		}
		else if (color == ColorID.BLUE)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_blue_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_blue);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_blue_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_blue_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_blue_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_blue_o);
				}	
			}
		}
		else if (color == ColorID.GREEN)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_green_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_green);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_green_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_green_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_green_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_green_o);
				}	
			}
		}
		else if (color == ColorID.YELLOW)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_yellow_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_yellow);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_yellow_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_yellow_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_yellow_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_yellow_o);
				}	
			}
		}
		else if (color == ColorID.ORANGE)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_orange_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_orange);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_orange_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_orange_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_orange_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_orange_o);
				}	
			}
		}
		else if (color == ColorID.KAKI)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_kaki_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_kaki);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_kaki_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_kaki_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_kaki_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_kaki_o);
				}	
			}
		}
		else if (color == ColorID.WHITE)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_white_used);
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_white);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_white_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_white_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_white_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_white_o);
				}	
			}
		}
		else if (color == ColorID.MAROON)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_maroon_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_maroon);
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_maroon_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_maroon_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_maroon_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_maroon_o);
				}	
			}
		}
		else if (color == ColorID.CYAN)
		{
			if (isExtremity)
			{
				if (isUsed)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_cyan_used);					
				}
				else
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.extremity_cyan);					
				}
			}
			for (Cardinality card : cardBlocs)
			{
				if (card == Cardinality.None)
					continue;
				
				if (card == Cardinality.North)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_cyan_n);
				}
				else if (card == Cardinality.South)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_cyan_s);
				}
				else if (card == Cardinality.East)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_cyan_e);
				}
				else if (card == Cardinality.West)
				{
					layers[currentLayerUp--] = r.getDrawable(R.drawable.path_cyan_o);
				}	
			}
		}
		
		LayerDrawable layerDrawable2 = new LayerDrawable(layers);
		drawables_[x][y] = layerDrawable2;

	}

}